package com.ice.work.study.design.pattern.observer;

import lombok.Getter;

import java.util.Observable;
import java.util.Observer;

/**
 * @Description 观察者模式测试
 * @Author DELL
 * @Create 2019-11-20
 * @Since 1.0.0
 */
public class CrudeOilFuture {
    public static void main(String[] args)
    {
        OilFuture oil=new OilFuture();
        //多方
        Observer bull=new Bull();
        //空方
        Observer bear=new Bear();
        oil.addObserver(bull);
        oil.addObserver(bear);
        oil.setPrice(10);
        oil.setPrice(-8);
    }

}
class Bull implements Observer {
    @Override
    public void update(Observable o, Object arg)
    {
        Float price=((Float)arg).floatValue();
        if(price>0)
        {
            System.out.println("油价上涨"+price+"元，多方高兴了！");
        }
        else
        {
            System.out.println("油价下跌"+(-price)+"元，多方伤心了！");
        }
    }
}
class Bear implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        Float price = ((Float) arg).floatValue();
        if (price > 0) {
            System.out.println("油价上涨" + price + "元，空方伤心了！");
        } else {
            System.out.println("油价下跌" + (-price) + "元，空方高兴了！");
        }
    }
}
class OilFuture  extends Observable {
    @Getter
    private float price;
    public void setPrice(float price)
    {
        //设置内部标志位，注明数据发生变化
        super.setChanged() ;
        //通知观察者价格改变了
        super.notifyObservers(price);
        this.price=price ;
    }
}
